capture program drop regtable
program regtable

syntax namelist [using], [tex keep(varlist) mlabels(namelist) cells(name) stats(namelist)] 

qui { 

	preserve
	
	label var tmissing "$\Delta T$ missing"
	*label var anypopF0 "Population $>0$"
	*label var roadpcttotF0 "Area $<0.5$ km from road"
	*label var totlntriF0 "Area $\times$ Ln(Top. Ruggedness Index)"


	if "`tex'" != "" {
		local style `"style(tex) replace"'
		}
	else local using ""
	
	if "`keep'" != "" {
		foreach var of varlist `keep' {
			local keepvars `keepvars' `var'
			}
		local keepopt `"keep(`keepvars')"'
		}
	
	if "`tex'" == "tex" {
		
		local var1: word 1 of `keep'
		local label: variable label `var1'
		label var `var1' "`label'"
		
		foreach var of varlist t? {
			local label: variable label `var'
			foreach var of varlist `var'* {
				label var `var' "\hspace{0.5em}\T `label'"
				}
			}	
		}
		
	noi di "`cells'"
	if "`cells'" == "columns" {
		local cells = `"cells("b(fmt(%9.2gc) star) se(fmt(%9.2gc) par([ ]))")"' 
		local span = `"span prefix(\multicolumn{@span}{c}{) suffix(})"'
		}
	else local cells = `"cells(b(fmt(%9.2gc) star) se(fmt(%9.2gc) par([ ])))"'
		
	if "`mlabels'" == "" local modellabs `"mlabels(,none)"'
	else {
		foreach name in `mlabels' {
			local lablist `lablist' "`name'"
			}
		local modellabs `"mlabels(`lablist', `span')"'
		}	
		
	noi estout `namelist' `using', `cells' ///
		`style' collabels(none) label starlevels(+ 0.10 * 0.05 ** 0.01) `keepopt' ///
		stats(`stats' firefe N numfires, fmt(%9.3gc %9.3gc) ///
			labels("\midrule Fire fixed effects" "Number obs." "Number fires")) /// 
		refcat(t2F0 "Bins of ln($\Delta T$)", nolabel) ///
		`modellabs' ///	
		
	restore
	
	}

end
